/*
 * Copyright (c) 2006-2018, Synwit Technology Co.,Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-05-31     ZYH          first version
 * 2018-12-10     Zohar_Lee    修复bug
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <rthw.h>

typedef void (*pin_callback_t)(void *args);
struct pin
{
    uint32_t package_index;
    const char *name;
    GPIO_TypeDef *port;
    uint32_t group_index;
    IRQn_Type irq;
    rt_uint32_t irq_mode;
    pin_callback_t callback;
    void *callback_args;
};
typedef struct pin pin_t;

//引脚编号、引脚描述、GPIO、PIN、中断、其他说明
#define SWM32_PIN(a, b, c, d, ...)           \
    {                                        \
        a, #b, GPIO##c, d, IRQ0_IRQ, 0, 0, 0 \
    }
#define GPIO0 ((GPIO_TypeDef *)(0))
#define GPIO0_IRQn (GPIOA0_IRQn)

#ifdef SWM181DBU6

#else
#ifdef SWM181CBT6
static pin_t swm32_pin_map[] =
    {
        SWM32_PIN(0, None, 0, 0, NO_FUNC, None, None, None),
        SWM32_PIN(1, RESET, 0, 0, NO_FUNC, None, None, RESET),
        SWM32_PIN(2, A11, A, 11, 0, FUNC, SCLK0, SEG21, GPIO),
        SWM32_PIN(3, A10, A, 10, FUNC, MOSI0, SEG20, GPIO),
        SWM32_PIN(4, A9, A, 9, FUNC, MISO0, SEG19, GPIO),
        SWM32_PIN(5, A8, A, 8, FUNC, SS_N0, SEG18, GPIO),
        SWM32_PIN(6, A7, A, 7, FUNC, COM3, None, GPIO),
        SWM32_PIN(7, A6, A, 6, FUNC, COM2, CP0P, GPIO),
        SWM32_PIN(8, A5, A, 5, FUNC, COM1, CP0N, GPIO),
        SWM32_PIN(9, A4, A, 4, FUNC, COM0, None, GPIO),
        SWM32_PIN(10, A3, A, 3, FUNC, SWDIO, None, SWD),
        SWM32_PIN(11, A2, A, 2, FUNC, SWCLK, None, SWD),
        SWM32_PIN(12, A1, A, 1, FUNC, None, None, ISP),
        SWM32_PIN(13, A0, A, 0, FUNC, WAKE_UP_IO, None, ISP),
        SWM32_PIN(14, XO, 0, 0, 0, NO_FUNC, None, None, OSC_OUT),
        SWM32_PIN(15, C1, C, 1, FUNC, XI, None, POSC_IN),
        SWM32_PIN(16, VSS33, 0, 0, 0, NO_FUNC, None, None, Power),
        SWM32_PIN(17, VDD33, 0, 0, 0, NO_FUNC, None, None, Power),
        SWM32_PIN(18, E4, E, 4, FUNC, ADC0_CH0, None, GPIO),
        SWM32_PIN(19, A15, A, 15, FUNC, ADC0_CH1, SCLK0, GPIO),
        SWM32_PIN(20, A14, A, 14, FUNC, ADC0_CH2, MOSI0 CP2P, GPIO),
        SWM32_PIN(21, A13, A, 13, FUNC, ADC0_CH3, MISO0 CP2N, GPIO),
        SWM32_PIN(22, A12, A, 12, FUNC, ADC0_CH4, SS_N0, GPIO),
        SWM32_PIN(23, AVDD0, 0, 0, 0, NO_FUNC, None, None, Power),
        SWM32_PIN(24, ADC0_REFP, 0, 0, 0, NO_FUNC, None, None, Power),
        SWM32_PIN(25, AVSS0, 0, 0, 0, NO_FUNC, None, None, Power),
        SWM32_PIN(26, C7, C, 7, FUNC, ADC0_CH5, ADC1_CH0_N SEG13 SCLK1, GPIO),
        SWM32_PIN(27, C6, C, 6, FUNC, ADC0_CH6, ADC1_CH0_P SEG12 MOSI1, GPIO),
        SWM32_PIN(28, D3, D, 3, FUNC, SEG17, None, GPIO),
        SWM32_PIN(29, D2, D, 2, FUNC, SEG16, None, GPIO),
        SWM32_PIN(30, AVSS1, 0, 0, 0, NO_FUNC, None, None, Power),
        SWM32_PIN(31, ADC1_REFP, 0, 0, 0, NO_FUNC, None, None, Power),
        SWM32_PIN(32, AVDD1, 0, 0, 0, NO_FUNC, None, None, Power),
        SWM32_PIN(33, D1, D, 1, FUNC, ADC1_CH4_N, SEG15, GPIO),
        SWM32_PIN(34, D0, D, 0, FUNC, ADC1_CH4_P, SEG14, GPIO),
        SWM32_PIN(35, C5, C, 5, FUNC, ADC1_CH1_N, SEG11, GPIO),
        SWM32_PIN(36, C4, C, 4, FUNC, ADC1_CH1_P, SEG10, GPIO),
        SWM32_PIN(37, C3, C, 3, FUNC, ADC1_CH3_N, None, GPIO),
        SWM32_PIN(38, C2, C, 2, FUNC, ADC1_CH3_P, None, GPIO),
        SWM32_PIN(39, B9, B, 9, FUNC, SEG9, None, GPIO),
        SWM32_PIN(40, B8, B, 8, FUNC, SEG8, None, GPIO),
        SWM32_PIN(41, B7, B, 7, FUNC, SEG7, None, GPIO),
        SWM32_PIN(42, B6, B, 6, FUNC, SEG6, None, GPIO),
        SWM32_PIN(43, B5, B, 5, FUNC, SEG5, None, GPIO),
        SWM32_PIN(44, B4, B, 4, FUNC, SEG4, None, GPIO),
        SWM32_PIN(45, B3, B, 3, FUNC, SEG3, CP1P, GPIO),
        SWM32_PIN(46, B2, B, 2, FUNC, SEG2, SP1N, GPIO),
        SWM32_PIN(47, B1, B, 1, FUNC, SEG1, None, GPIO),
        SWM32_PIN(48, B0, B, 0, FUNC, SEG0, None, GPIO)};
#else
#ifdef SWM181RCT6

#endif //SWM181RCT6

#endif //SWM181CBT6

#endif //SWM181DBU6

#define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
static pin_t *get_pin(uint8_t pin)
{
    pin_t *index;
    if (pin < ITEM_NUM(swm32_pin_map))
    {
        index = &swm32_pin_map[pin];
        if (index->port == GPIO0)
            index = RT_NULL;
    }
    else
    {
        index = RT_NULL;
    }
    return index;
};

static void swm320_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
{
    pin_t *index;
    index = get_pin(pin);
    if (index == RT_NULL)
    {
        return;
    }
    if (value)
    {
        GPIO_SetBit(index->port, index->group_index);
    }
    else
    {
        GPIO_ClrBit(index->port, index->group_index);
    }
}

static int swm320_pin_read(rt_device_t dev, rt_base_t pin)
{
    pin_t *index;
    index = get_pin(pin);
    if (index == RT_NULL)
    {
        return PIN_LOW;
    }
    return GPIO_GetBit(index->port, index->group_index);
}

static void swm320_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
{
    pin_t *index;
    int dir = 0;
    int pull_up = 0;
    int pull_down = 0;
    int open_drain = 0;

    index = get_pin(pin);
    if (index == RT_NULL)
    {
        return;
    }
    /* Configure GPIO_InitStructure */
    if (mode == PIN_MODE_OUTPUT)
    {
        /* output setting */
        dir = 1;
    }
    else if (mode == PIN_MODE_INPUT)
    {
        /* input setting: not pull. */
        dir = 0;
    }
    else if (mode == PIN_MODE_INPUT_PULLUP)
    {
        /* input setting: pull up. */
        dir = 0;
        pull_up = 1;
    }
    else if (mode == PIN_MODE_INPUT_PULLDOWN)
    {
        /* input setting: pull down. */
        dir = 0;
        pull_down = 1;
    }
    else if (mode == PIN_MODE_OUTPUT_OD)
    {
        /* output setting: od. */
        dir = 1;
        pull_up = 1;
        open_drain = 1;
    }
    GPIO_Init(index->port,
              index->group_index,
              dir,
              pull_up,
              pull_down,
              open_drain);
}

static rt_err_t swm320_pin_attach_irq(struct rt_device *device,
                                      rt_int32_t pin,
                                      rt_uint32_t mode,
                                      pin_callback_t cb,
                                      void *args)
{
    pin_t *index;
    rt_base_t level;
    index = get_pin(pin);
    if (index == RT_NULL)
    {
        return RT_EINVAL;
    }
    level = rt_hw_interrupt_disable();
    index->callback = cb;
    index->callback_args = args;
    index->irq_mode = mode;

    rt_hw_interrupt_enable(level);
    return RT_EOK;
}

static rt_err_t swm320_pin_dettach_irq(struct rt_device *device, rt_int32_t pin)
{
    pin_t *index;
    rt_base_t level;
    index = get_pin(pin);
    if (index == RT_NULL)
    {
        return RT_EINVAL;
    }
    level = rt_hw_interrupt_disable();
    index->callback = 0;
    index->callback_args = 0;
    index->irq_mode = 0;
    rt_hw_interrupt_enable(level);
    return RT_EOK;
}

static rt_err_t swm320_pin_irq_enable(struct rt_device *device,
                                      rt_base_t pin,
                                      rt_uint32_t enabled)
{
    pin_t *index;
    rt_base_t level = 0;
    index = get_pin(pin);
    if (index == RT_NULL)
    {
        return RT_EINVAL;
    }
    if (enabled == PIN_IRQ_ENABLE)
    {

        switch (index->irq_mode)
        {
        case PIN_IRQ_MODE_RISING:
            GPIO_Init(index->port, index->group_index, 0, 0, 1, 0);
            EXTI_Init(index->port, index->group_index, EXTI_RISE_EDGE);
            break;
        case PIN_IRQ_MODE_FALLING:
            GPIO_Init(index->port, index->group_index, 0, 1, 0, 0);
            EXTI_Init(index->port, index->group_index, EXTI_FALL_EDGE);
            break;
        case PIN_IRQ_MODE_RISING_FALLING:
            GPIO_Init(index->port, index->group_index, 0, 1, 1, 0);
            EXTI_Init(index->port, index->group_index, EXTI_BOTH_EDGE);
            break;
        case PIN_IRQ_MODE_HIGH_LEVEL:
            GPIO_Init(index->port, index->group_index, 0, 0, 1, 0);
            EXTI_Init(index->port, index->group_index, EXTI_HIGH_LEVEL);
            break;
        case PIN_IRQ_MODE_LOW_LEVEL:
            GPIO_Init(index->port, index->group_index, 0, 1, 0, 0);
            EXTI_Init(index->port, index->group_index, EXTI_LOW_LEVEL);
            break;
        default:
            rt_hw_interrupt_enable(level);
            return RT_EINVAL;
        }

        if (GPIOA == index->port)
        {
            IRQ_Connect(IRQ16_31_GPIOA, index->irq, 3);
        }
        else if (GPIOB == index->port)
        {
            IRQ_Connect(IRQ16_31_GPIOB, index->irq, 3);
        }
        else if (GPIOC == index->port)
        {
            IRQ_Connect(IRQ16_31_GPIOC, index->irq, 3);
        }
        else if (GPIOD == index->port)
        {
            IRQ_Connect(IRQ16_31_GPIOD, index->irq, 3);
        }
        else if (GPIOE == index->port)
        {
            IRQ_Connect(IRQ16_31_GPIOE, index->irq, 3);
        }

        level = rt_hw_interrupt_disable();
        NVIC_EnableIRQ(index->irq);
        EXTI_Open(index->port, index->group_index);
        rt_hw_interrupt_enable(level);
    }
    else if (enabled == PIN_IRQ_DISABLE)
    {
        NVIC_DisableIRQ(index->irq);
        EXTI_Close(index->port, index->group_index);
    }
    else
    {
        return RT_ENOSYS;
    }
    return RT_EOK;
}

const static struct rt_pin_ops swm320_pin_ops =
    {
        swm320_pin_mode,
        swm320_pin_write,
        swm320_pin_read,
        swm320_pin_attach_irq,
        swm320_pin_dettach_irq,
        swm320_pin_irq_enable};

int rt_hw_pin_init(void)
{
    int result;
    result = rt_device_pin_register("pin", &swm320_pin_ops, RT_NULL);
    return result;
}
INIT_BOARD_EXPORT(rt_hw_pin_init);

void IRQ24_Handler(void)
{
    static int gpio[24];
    int index = 0;
    static int init = 0;
    pin_t *pin;
    /* enter interrupt */
    rt_interrupt_enter();
    if (init == 0)
    {
        init = 1;
        for (pin = &swm32_pin_map[1];
             pin->package_index < ITEM_NUM(swm32_pin_map);
             pin++)
        {
            if (pin->port == GPIOA)
            {
                gpio[index] = pin->package_index;
                index++;
                RT_ASSERT(index <= 24)
            }
        }
    }
    for (index = 0; index < 24; index++)
    {
        pin = get_pin(gpio[index]);
        if (index != RT_NULL)
        {
            if (EXTI_State(pin->port, pin->group_index))
            {
                EXTI_Clear(pin->port, pin->group_index);
                if (pin->callback)
                {
                    pin->callback(pin->callback_args);
                }
            }
        }
    }
    /* leave interrupt */
    rt_interrupt_leave();
}

void IRQ25_Handler(void)
{
    static int gpio[24];
    int index = 0;
    static int init = 0;
    pin_t *pin;
    /* enter interrupt */
    rt_interrupt_enter();
    if (init == 0)
    {
        init = 1;
        for (pin = &swm32_pin_map[1];
             pin->package_index < ITEM_NUM(swm32_pin_map);
             pin++)
        {
            if (pin->port == GPIOB)
            {
                gpio[index] = pin->package_index;
                index++;
                RT_ASSERT(index <= 24)
            }
        }
    }
    for (index = 0; index < 24; index++)
    {
        pin = get_pin(gpio[index]);
        if (index != RT_NULL)
        {
            if (EXTI_State(pin->port, pin->group_index))
            {
                EXTI_Clear(pin->port, pin->group_index);
                if (pin->callback)
                {
                    pin->callback(pin->callback_args);
                }
            }
        }
    }
    /* leave interrupt */
    rt_interrupt_leave();
}

void IRQ26_Handler(void)
{
    static int gpio[24];
    int index = 0;
    static int init = 0;
    pin_t *pin;
    /* enter interrupt */
    rt_interrupt_enter();
    if (init == 0)
    {
        init = 1;
        for (pin = &swm32_pin_map[1];
             pin->package_index < ITEM_NUM(swm32_pin_map);
             pin++)
        {
            if (pin->port == GPIOC)
            {
                gpio[index] = pin->package_index;
                index++;
                RT_ASSERT(index <= 24)
            }
        }
    }
    for (index = 0; index < 24; index++)
    {
        pin = get_pin(gpio[index]);
        if (index != RT_NULL)
        {
            if (EXTI_State(pin->port, pin->group_index))
            {
                EXTI_Clear(pin->port, pin->group_index);
                if (pin->callback)
                {
                    pin->callback(pin->callback_args);
                }
            }
        }
    }
    /* leave interrupt */
    rt_interrupt_leave();
}

void IRQ27_Handler(void)
{
    static int gpio[24];
    int index = 0;
    static int init = 0;
    pin_t *pin;
    /* enter interrupt */
    rt_interrupt_enter();
    if (init == 0)
    {
        init = 1;
        for (pin = &swm32_pin_map[1];
             pin->package_index < ITEM_NUM(swm32_pin_map);
             pin++)
        {
            if (pin->port == GPIOD)
            {
                gpio[index] = pin->package_index;
                index++;
                RT_ASSERT(index <= 24)
            }
        }
    }
    for (index = 0; index < 24; index++)
    {
        pin = get_pin(gpio[index]);
        if (index != RT_NULL)
        {
            if (EXTI_State(pin->port, pin->group_index))
            {
                EXTI_Clear(pin->port, pin->group_index);
                if (pin->callback)
                {
                    pin->callback(pin->callback_args);
                }
            }
        }
    }
    /* leave interrupt */
    rt_interrupt_leave();
}

void IRQ28_Handler(void)
{
    static int gpio[24];
    int index = 0;
    static int init = 0;
    pin_t *pin;
    /* enter interrupt */
    rt_interrupt_enter();
    if (init == 0)
    {
        init = 1;
        for (pin = &swm32_pin_map[1]; pin->package_index < ITEM_NUM(swm32_pin_map); pin++)
        {
            if (pin->port == GPIOE)
            {
                gpio[index] = pin->package_index;
                index++;
                RT_ASSERT(index <= 24)
            }
        }
    }
    for (index = 0; index < 24; index++)
    {
        pin = get_pin(gpio[index]);
        if (index != RT_NULL)
        {
            if (EXTI_State(pin->port, pin->group_index))
            {
                EXTI_Clear(pin->port, pin->group_index);
                if (pin->callback)
                {
                    pin->callback(pin->callback_args);
                }
            }
        }
    }
    /* leave interrupt */
    rt_interrupt_leave();
}
